/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZEmailAddress;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.db.DbOutOfOffice;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.util.Date;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestOutOfOffice
/*     */   extends TestCase
/*     */ {
/*     */   private DbPool.DbConnection mConn;
/*     */   private Mailbox mMbox;
/*     */   private String mOriginalFromAddress;
/*     */   private String mOriginalFromDisplay;
/*     */   private String mOriginalReplyEnabled;
/*     */   private String mOriginalFromDate;
/*     */   private String mOriginalUntilDate;
/*     */   private String mOriginalAllowAnyFrom;
/*     */   private String mOriginalReplyToAddress;
/*     */   private String mOriginalReplyToDisplay;
/*     */   private String mOriginalReplyToEnabled;
/*  55 */   private static String NAME_PREFIX = TestOutOfOffice.class.getSimpleName();
/*  56 */   private static String RECIPIENT_NAME = "user1";
/*  57 */   private static String SENDER_NAME = "user2";
/*  58 */   private static String RECIPIENT1_ADDRESS = "TestOutOfOffice1@example.zimbra.com";
/*  59 */   private static String RECIPIENT2_ADDRESS = "TestOutOfOffice2@example.zimbra.com";
/*     */   
/*     */   protected void setUp() throws Exception
/*     */   {
/*  63 */     super.setUp();
/*     */     
/*  65 */     this.mMbox = TestUtil.getMailbox(RECIPIENT_NAME);
/*  66 */     this.mConn = DbPool.getConnection();
/*     */     
/*  68 */     Account recipient = TestUtil.getAccount(RECIPIENT_NAME);
/*  69 */     this.mOriginalFromAddress = recipient.getPrefFromAddress();
/*  70 */     this.mOriginalFromDisplay = recipient.getPrefFromDisplay();
/*  71 */     this.mOriginalAllowAnyFrom = TestUtil.getAccountAttr(RECIPIENT_NAME, "zimbraAllowAnyFromAddress");
/*  72 */     this.mOriginalReplyEnabled = TestUtil.getAccountAttr(RECIPIENT_NAME, "zimbraPrefOutOfOfficeReplyEnabled");
/*     */     
/*  74 */     this.mOriginalFromDate = recipient.getPrefOutOfOfficeFromDateAsString();
/*  75 */     this.mOriginalUntilDate = recipient.getPrefOutOfOfficeUntilDateAsString();
/*  76 */     this.mOriginalReplyToAddress = recipient.getPrefReplyToAddress();
/*  77 */     this.mOriginalReplyToDisplay = recipient.getPrefReplyToDisplay();
/*  78 */     this.mOriginalReplyToEnabled = TestUtil.getAccountAttr(RECIPIENT_NAME, "zimbraPrefReplyToEnabled");
/*     */     
/*  80 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testRowExists() throws Exception {
/*  84 */     long fiveDaysAgo = System.currentTimeMillis() - 432000000L - 100000L;
/*     */     
/*  86 */     DbOutOfOffice.setSentTime(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, fiveDaysAgo);
/*  87 */     this.mConn.commit();
/*  88 */     assertFalse("1 day", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 86400000L));
/*  89 */     assertFalse("4 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 345600000L));
/*  90 */     assertFalse("5 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 432000000L));
/*  91 */     assertTrue("6 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 518400000L));
/*  92 */     assertTrue("100 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 8640000000L));
/*     */   }
/*     */   
/*     */   public void testRowDoesntExist() throws Exception {
/*  96 */     assertFalse("1 day", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 86400000L));
/*  97 */     assertFalse("5 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 432000000L));
/*  98 */     assertFalse("100 days", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 8640000000L));
/*     */   }
/*     */   
/*     */   public void testPrune() throws Exception
/*     */   {
/* 103 */     long fiveDaysAgo = System.currentTimeMillis() - 432000000L - 100000L;
/* 104 */     long sixDaysAgo = System.currentTimeMillis() - 518400000L - 100000L;
/*     */     
/* 106 */     DbOutOfOffice.setSentTime(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, fiveDaysAgo);
/* 107 */     DbOutOfOffice.setSentTime(this.mConn, this.mMbox, RECIPIENT2_ADDRESS, sixDaysAgo);
/* 108 */     this.mConn.commit();
/*     */     
/*     */ 
/* 111 */     DbOutOfOffice.prune(this.mConn, 518400000L);
/* 112 */     this.mConn.commit();
/*     */     
/*     */ 
/* 115 */     assertTrue("recipient1", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT1_ADDRESS, 518400000L));
/* 116 */     assertFalse("recipient2", DbOutOfOffice.alreadySent(this.mConn, this.mMbox, RECIPIENT2_ADDRESS, 604800000L));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testPrefFromAddress()
/*     */     throws Exception
/*     */   {
/* 125 */     String newFromAddress = TestUtil.getAddress("testPrefFromAddress");
/* 126 */     String newFromDisplay = NAME_PREFIX + " testPrefFromAddress";
/* 127 */     String newReplyToAddress = TestUtil.getAddress("testReplyToAddress");
/* 128 */     String newReplyToDisplay = NAME_PREFIX + " testReplyToAddress";
/*     */     
/*     */ 
/* 131 */     Account recipient = TestUtil.getAccount(RECIPIENT_NAME);
/* 132 */     long now = System.currentTimeMillis();
/* 133 */     recipient.setPrefOutOfOfficeFromDate(new Date(now));
/* 134 */     recipient.setPrefOutOfOfficeUntilDate(new Date(now + 86400000L));
/* 135 */     recipient.setPrefOutOfOfficeReplyEnabled(true);
/*     */     
/*     */ 
/* 138 */     recipient.setAllowAnyFromAddress(false);
/* 139 */     recipient.setPrefFromDisplay(newFromDisplay);
/* 140 */     recipient.setPrefReplyToAddress(newReplyToAddress);
/* 141 */     recipient.setPrefReplyToDisplay(newReplyToDisplay);
/* 142 */     recipient.setPrefReplyToEnabled(false);
/* 143 */     String subject = NAME_PREFIX + " testPrefFromAddress 1";
/* 144 */     ZMailbox senderMbox = TestUtil.getZMailbox(SENDER_NAME);
/* 145 */     TestUtil.sendMessage(senderMbox, RECIPIENT_NAME, subject);
/* 146 */     ZMessage reply = TestUtil.waitForMessage(senderMbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/* 149 */     ZEmailAddress fromAddress = getAddress(reply, "f");
/* 150 */     assertEquals(recipient.getName(), fromAddress.getAddress());
/* 151 */     assertEquals(newFromDisplay, fromAddress.getPersonal());
/* 152 */     assertNull(getAddress(reply, "r"));
/*     */     
/* 154 */     DbOutOfOffice.clear(this.mConn, this.mMbox);
/* 155 */     this.mConn.commit();
/*     */     
/*     */ 
/* 158 */     recipient.setPrefFromAddress(newFromAddress);
/* 159 */     recipient.setAllowAnyFromAddress(false);
/* 160 */     recipient.setPrefReplyToEnabled(true);
/* 161 */     subject = NAME_PREFIX + " testPrefFromAddress 2";
/* 162 */     TestUtil.sendMessage(senderMbox, RECIPIENT_NAME, subject);
/* 163 */     reply = TestUtil.waitForMessage(senderMbox, "in:inbox subject:\"" + subject + "\"");
/*     */     
/*     */ 
/* 166 */     fromAddress = getAddress(reply, "f");
/* 167 */     assertEquals(recipient.getName(), fromAddress.getAddress());
/* 168 */     assertEquals(recipient.getDisplayName(), fromAddress.getPersonal());
/*     */     
/* 170 */     ZEmailAddress replyToAddress = getAddress(reply, "r");
/* 171 */     assertEquals(newReplyToAddress, replyToAddress.getAddress());
/* 172 */     assertEquals(newReplyToDisplay, replyToAddress.getPersonal());
/*     */     
/* 174 */     DbOutOfOffice.clear(this.mConn, this.mMbox);
/* 175 */     this.mConn.commit();
/*     */     
/*     */ 
/* 178 */     recipient.setAllowAnyFromAddress(true);
/* 179 */     subject = NAME_PREFIX + " testPrefFromAddress 3";
/* 180 */     TestUtil.sendMessage(senderMbox, RECIPIENT_NAME, subject);
/* 181 */     reply = TestUtil.waitForMessage(senderMbox, "in:inbox subject:\"" + subject + "\"");
/* 182 */     ZimbraLog.test.info("Second reply:\n" + TestUtil.getContent(senderMbox, reply.getId()));
/*     */     
/*     */ 
/* 185 */     fromAddress = getAddress(reply, "f");
/* 186 */     assertEquals(newFromAddress, fromAddress.getAddress());
/* 187 */     assertEquals(newFromDisplay, fromAddress.getPersonal());
/*     */     
/* 189 */     replyToAddress = getAddress(reply, "r");
/* 190 */     assertEquals(newReplyToAddress, replyToAddress.getAddress());
/* 191 */     assertEquals(newReplyToDisplay, replyToAddress.getPersonal());
/*     */   }
/*     */   
/*     */   private ZEmailAddress getAddress(ZMessage msg, String type) {
/* 195 */     for (ZEmailAddress address : msg.getEmailAddresses()) {
/* 196 */       if (address.getType().equals(type)) {
/* 197 */         return address;
/*     */       }
/*     */     }
/* 200 */     return null;
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 206 */     cleanUp();
/* 207 */     DbPool.quietClose(this.mConn);
/*     */     
/* 209 */     Account recipient = TestUtil.getAccount(RECIPIENT_NAME);
/* 210 */     recipient.setPrefFromAddress(this.mOriginalFromAddress);
/* 211 */     recipient.setPrefFromDisplay(this.mOriginalFromDisplay);
/* 212 */     TestUtil.setAccountAttr(RECIPIENT_NAME, "zimbraAllowAnyFromAddress", this.mOriginalAllowAnyFrom);
/*     */     
/* 214 */     TestUtil.setAccountAttr(RECIPIENT_NAME, "zimbraPrefOutOfOfficeReplyEnabled", this.mOriginalReplyEnabled);
/* 215 */     TestUtil.setAccountAttr(RECIPIENT_NAME, "zimbraPrefOutOfOfficeFromDate", this.mOriginalFromDate);
/* 216 */     TestUtil.setAccountAttr(RECIPIENT_NAME, "zimbraPrefOutOfOfficeUntilDate", this.mOriginalUntilDate);
/* 217 */     recipient.setPrefReplyToAddress(this.mOriginalReplyToAddress);
/* 218 */     recipient.setPrefReplyToDisplay(this.mOriginalReplyToDisplay);
/* 219 */     TestUtil.setAccountAttr(RECIPIENT_NAME, "zimbraPrefReplyToEnabled", this.mOriginalReplyToEnabled);
/*     */     
/* 221 */     super.tearDown();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 226 */     DbOutOfOffice.clear(this.mConn, this.mMbox);
/* 227 */     this.mConn.commit();
/* 228 */     TestUtil.deleteTestData(SENDER_NAME, NAME_PREFIX);
/* 229 */     TestUtil.deleteTestData(RECIPIENT_NAME, NAME_PREFIX);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestOutOfOffice.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */